exports.key = key;

// 键盘相关封装
function key(keycode) {
    let key = {};
    key.code = keycode;
    key.isDown = false;
    key.isUp = true;


    key.press = undefined;
    key.release = undefined;

    key.downHandler = event => {
        if (key.code == event.keyCode) {
            if (key.isUp && key.press) {
                key.press();
            }
            key.isUp = false
            key.isDown = true;
        }
        event.preventDefault();
    }

    key.upHandler = event => {
        if (key.code == event.keyCode) {
            if (key.isDown && key.release) {
                key.release();
            }
            key.isUp = true;
            key.isDown = false;
        }
    }

    window.addEventListener("keydown", key.downHandler.bind(key), false);

    window, addEventListener("keyup", key.upHandler.bind(key), false);

    return key;
}